首页 > 试题广场 >

Excel 表列序号

[编程题]Excel 表列序号
  • 热度指数:1677 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
现给你一个仅由大写字母组成的字符串S,表示Excel表格里面的列名称。比如字符串A的序号为1,字符串B的序号为2,字符串Z的序号为26,字符串 AA的序号为27,请输出该字符串的序号。

S在字符串AXFD范围内
示例1

输入

"C"

输出

3

说明

字符串c的序号为3 
示例2

输入

"AB"

输出

28

说明

字符串ab的序号为28 
class Solution:
    def getNumber(self , S: str) -> int:
        # write code here
        import string
        res = 0
        for i, v in enumerate(S[::-1]):
            res += (string.ascii_uppercase.index(v)+1)*26**i
        return res

发表于 2022-07-06 22:19:50 回复(0)
import string
class Solution:
    def getNumber(self , S: str) -> int:
        # write code here
        d = dict(zip(string.ascii_uppercase, range(1,27)))
        res = 0
        for i, ch in enumerate(S):
            res += d[ch]*26**(len(S)-1-i)
        return res

发表于 2022-04-22 16:02:00 回复(0)
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param S string字符串 
# @return int整型
#
class Solution:
    def getNumber(self , S: str) -> int:
        # write code here
        d = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        n = 0
        i = 0
        S = S[::-1]
        for c in S:
            cn = d.index(c) + 1
            for j in range(i):
                cn = cn * 26
            n += cn
            i += 1
        return n
            

发表于 2022-03-10 10:32:37 回复(0)

问题信息

难度:
3条回答 2029浏览

热门推荐

通过挑战的用户

查看代码